package edu.ncsu.csc.camera.com;

// for SPOT
import com.sun.squawk.util.StringTokenizer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
// for PC
//import java.util.StringTokenizer;

/**
 * A CMUCameraPacket that represents statistical information from the CMUCam2
 * @author Richard Pospesel
 * @author Douglas McClusky
 */

public class SPacket extends CMUCameraPacket
{
	private int[] means;
	private int[] deviations;
	
	/**
     * Constructs an SPacket from the means and deviations of each of the three channels (Red, Green, Blue)
     * @param means [3]
     * @param deviations [3]
     */

    public SPacket (int[] means, int[] deviations)
	{
        init(means, deviations);
	}

    /**
     * Constructs an SPacket from the String encoding returned by the CMUCam2:
     * S Rmean Gmean Bmean Rdeviation Gdeviation Bdeviation
     * @param str
     * @throws java.lang.IllegalArgumentException
     */

	public SPacket (String str) throws IllegalArgumentException
	{
		StringTokenizer st = new StringTokenizer(str, " \r");
		int tokens = st.countTokens();
		if(tokens == 7 && st.nextToken().equals("S"))
			{
				int[] _means = new int[3];
				_means[0] = Integer.parseInt(st.nextToken());
				_means[1] = Integer.parseInt(st.nextToken());
				_means[2] = Integer.parseInt(st.nextToken());
				
				int[] _deviations = new int[3];
				_deviations[0] = Integer.parseInt(st.nextToken());
				_deviations[1] = Integer.parseInt(st.nextToken());
				_deviations[2] = Integer.parseInt(st.nextToken());
				
				init(_means, _deviations);
		} else {
            throw new IllegalArgumentException("Malformed TPacket String");
        }
	}

    /**
     * Constracts an SPacket from the output of SPacket.toByteArray()
     * @param in
     */

    public SPacket(byte[] in) {
        try {
            ByteArrayInputStream bin = new ByteArrayInputStream(in);
            DataInputStream dis = new DataInputStream(bin);
            int[] mns = new int[3], devs = new int[3];
            for (int i = 0; i < 3; i++) {
                mns[i] = dis.readInt();
            }
            for (int i = 0; i < 3; i++) {
                devs[i] = dis.readInt();
            }
            init(mns, devs);
        } catch (IOException iOException) {
        }
    }

    private void init(int[] means, int[] deviations) {
        super.type = super.S_PACKET;
        this.means = means;
		this.deviations = deviations;
    }

    /**
     * Displays the SPacket as a string encoded as the CMUCam2 would:
     * S Rmean Gmean Bmean Rdeviation Gdeviation Bdeviation
     * @return
     */
    
	public String toString()
	{
		String result = "S ";
		for(int k = 0; k < 3; k++)
			result += means[k] + " ";
		for(int k = 0; k < 3; k++)
			result += deviations[k] + " ";
		return result;
	}

    public int getRMean() {
        return means[0];
    }

    public int getGMean() {
        return means[1];
    }

    public int getBMean() {
        return means[2];
    }

    public int getRDeviation() {
        return deviations[0];
    }

    public int getGDeviation() {
        return deviations[1];
    }

    public int getBDeviation() {
        return deviations[2];
    }

    /**
     * A byte array representation of this SPacket for communication purposes
     * @return
     */

    public byte[] toByteArray() {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bout);
        try {
            for (int i = 0; i < means.length; i++) {
                dos.writeInt(means[i]);

            }
            for (int i = 0; i < deviations.length; i++) {
                dos.writeInt(deviations[i]);
              }
            dos.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return bout.toByteArray();
    }
}